#!/bin/sh

#172.24.64.107

user='dump'
passwd='dump123'
user1="repsync"
passwd1="repsync123"
port='3306'
slave_db_name="cm5_spam"
slave_host="172.25.89.28"
ndiff=0
n_print_interval=1000
n_check_interval=100

function my_echo {
    local str=$1
    echo "${str}"
}

function max_value {
    local v1=$1
    local v2=$2
    local max=$1
    if (($max < $2)) ;then
        max=$2
     fi
    echo $max
}

function sync_data {
    local nhosts=$1
    local ndbs=$2
    local ntbls=$3
    local flag=$4
    local cmd=
    local cmd2=
    local ret=
    local ret2=

    my_echo "dumping hosts ..."
    local nhost_idx
    local ndb_idx
    local ntbl_idx
    let nhost_idx=nhosts-1
    let ndb_idx=ndbs-1
    let ntbl_idx=ntbls-1

    rm -rf sync_pos.log

    local cmd=
    for master_index in `seq 0 $nhost_idx`
    do
        local master_id=$((${master_index}+43))
        local master_name="172.24.64.107"

        echo "processing: stop slave/flush logs/dump/import/start slave/show logs on $master_name ...."
        mysql="mysql -h${master_name} -u${user} -p${passwd} -P${port}"
        cmd="stop slave;"
       `$mysql -e "$cmd"`
       `$mysql -e "flush logs"`

        for db_index in `seq 0 $ndb_idx`
        do
            local cur_db_index=$((${master_index} * 2 + ${db_index}))

            local db_name="spam_manager"

            `mysqldump --flush-logs --single-transaction -h${master_name} -u${user} -p${passwd} -P${port} ${db_name} | tee ${db_name}.sql | mysql -h${slave_host} -u${user1} -p${passwd1} -P${port} ${slave_db_name}` &
             

            # dump all tables in database
            #`mysqldump -h${master_name} -u${user} -p${passwd} -P${port} ${db_name} | tee ${db_name}.sql ` &
            #`mysqldump -h${master_name} -u${user} -p${passwd} -P${port} -d ${db_name} | tee ${db_name}.sql`
            #cat ${db_name}.sql | mysql -h${slave_host} -u${user1} -p${passwd1} -P${port}`
            for tbl_index in `seq 0 $ntbl_idx`
            do
                local cur_tbl_index=$((${cur_db_index} * 1 + ${tbl_index}))
                local tbl_name=`printf "spamauction_%04d" ${cur_tbl_index}`
                #echo "# dumping  ${db_name} : ${tbl_name} "
                #`mysqldump -h${master_name} -u${user} -p${passwd} -P${port} ${db_name} ${tbl_name} | tee ${tbl_name}.sql | mysql -h${slave_host} -u${user1} -p${passwd1} -P${port} ${slave_db_name}` 
                tbl_name=`printf "spamuser_%04d" ${cur_tbl_index}`
                #echo "# dumping  ${db_name} : ${tbl_name} "
                #mysqldump --flush-logs --single-transaction -h${master_name} -u${user} -p${passwd} -P${port} ${db_name} ${tbl_name} | tee ${tbl_name}.sql | mysql -h${slave_host} -u${user1} -p${passwd1} -P${port} ${slave_db_name}` 
 
                #echo "# import a special tables in database: ${db_name} . ${tbl_name} "
                #`mysql -h${slave_host} -u${user1} -p${passwd1} -P${port} ${slave_db_name} < ${tbl_name}.sql` 
                #`mysqldump -h${master_name} -u${user} -p${passwd} -P${port} -B ${db_name} --table ${tbl_name} -d | tee ${tbl_name}.sql ` 
        #cat ${tbl_name}.sql | mysql -h${slave_host} -u${user1} -p${passwd1} -P${port}` #&
            done
        done

    cmd="start slave;"
    `$mysql -e "${cmd}"`
    sleep 3
    cmd="show master logs;"
    $mysql -e "${cmd}" | tail -1 | tee -a sync_pos.log

    done
}

# usage:  sh $0 NHOSTS NDATABASES NTABLES
sync_data 1 1 128
#sync_data 1 1 2
wait
echo "Finish sync data!"

